<style>
    body {
        overflow: scroll;
    }
    .html{
        padding: 0 0 0 0 !important;
    }
    .column-text{
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
    }

    .column-checkbox1{
        width: 44px;
    }
    .column-classify-title{
        width: 300px;
        display: block;
    }
    .column-area{
        width: 44px;
    }
    .column-sku-psku_code{
        width: 70px;
    }
    .column-sku-psku_cn{
        width: 350px;
        display: block;
    }
    .column-recommend_num{
        width: 72px;
    }
    .column-recommend_remark{
        width: 64px;
    }
    .column-apply_num{
        width: 72px;
    }
    .column-apply_remark{
        width: 64px;
    }
    .column-approval_num{
        width: 72px;
    }
    .column-approval_remark{
        width: 64px;
    }
    .column-confirm_num{
        width: 72px;
    }
    .column-confirm_remark{
        width: 64px;
    }
    .column-status{
        width: 72px;
    }

    .column-avg3{
        width: 68px;
    }
    .column-avg7{
        width: 68px;
    }
    .column-avg15{
        width: 68px;
    }
    .column-avg30{
        width: 68px;
    }
    .column-avg{
        width: 68px;
    }

    .column-estimate_num0{
        width: 78px;
    }
    .column-estimate_num1{
        width: 78px;
    }
    .column-estimate_num2{
        width: 78px;
    }
    .column-estimate_num3{
        width: 78px;
    }
    .column-estimate_num4{
        width: 78px;
    }
    .column-estimate_num5{
        width: 78px;
    }
    .column-estimate_num6{
        width: 78px;
    }
    .column-estimate_num7{
        width: 78px;
    }
    .column-estimate_num8{
        width: 78px;
    }

    .column-fba_instant_num{
        width: 68px;
    }
    .column-fba_transit_num{
        width: 68px;
    }
    .column-cn_instant_num{
        width: 72px;
    }
    .column-cn_transit_num{
        width: 72px;
    }
    .column-os_instant_num{
        width: 86px;
    }
    .column-os_transit_num{
        width: 86px;
    }
    .column-stock_total{
        width: 73px;
    }
    .column-cn_pulic_instant_num{
        width: 114px;
    }
    .column-cn_stock_total{
        width: 114px;
    }
    .column-overstock_num{
        width: 156px;
    }
    .column-divert_overstock_num{
        width: 128px;
    }
    .column-divert_instant_num{
        width: 170px;
    }
    .column-divert_transit_num{
        width: 170px;
    }
    .column-defer_stock_num{
        width: 142px;
    }
</style>

<div>
    <table class="table table-hover grid-table">
        <thead style="visibility: collapse;">
        <tr>
            <th class="column-checkbox1">选项</th>
            <th class="column-classify-title column-text" style="padding-top: 16px;">品类</th>
            <th class="column-area">站点</th>
            <th class="column-sku-psku_code">SKU</th>
            <th class="column-sku-psku_cn column-text" style="padding-top: 9px;">SKU名称</th>
            <th class="column-recommend_num">推荐备货数量</th>
            <th class="column-apply_num">申请数量</th>
            <th class="column-apply_remark">申请备注</th>
            <th class="column-approval_num">审批数量</th>
            <th class="column-approval_remark">审批备注</th>
            <th class="column-confirm_num">确认数量</th>
            <th class="column-confirm_remark">确认备注</th>
            <th class="column-status">备货状态</th>

            <th class="column-current_sale_ratio">最近旺季销售比例</th>
            <th class="column-volume3_avg">最近3天平均销量</th>
            <th class="column-volume30_avg">最近30天平均销量</th>
            <th class="column-volume30_ratio">最近30天销售比例</th>
            <th class="column-ratio">确定后的销售比例</th>

            <th class="column-fba_instant_num">FBA在库</th>
            <th class="column-fba_transit_num">FBA在途</th>
            <th class="column-cn_instant_num">国内在库</th>
            <th class="column-cn_transit_num">国内在途</th>
            <th class="column-os_instant_num">海外仓在库</th>
            <th class="column-os_transit_num">海外仓在途</th>
            <th class="column-dispense_num">分配数量</th>
            <th class="column-dispense_surplus_num">分配剩余</th>
            <th class="column-stock_total">库存总量</th>

            @for($i = 0; $i < App\Models\SsBasic\AreaSkuEstimate::NEW_ESTIMATE_NUM; $i++)
                <th class="column-estimate_num{{$i}}">{{ date('y/m',strtotime("midnight first day of $i month")).'预估' }}</th>
            @endfor
            <th class="column-estimate_total">预估总量</th>
            <th class="column-estimate_growth_rate">同期增长率</th>

            <th class="column-cn_pulic_instant_num">公共在库数量</th>
            <th class="column-cn_stock_total">中国库存总量</th>
            <th class="column-overstock_num">备货推荐和总积压库存</th>
            <th class="column-divert_overstock_num">总库存可挪用积压</th>
            <th class="column-divert_instant_num">中国在库库存可挪用积压</th>
            <th class="column-divert_transit_num">中国在途库存可挪用积压</th>
            <th class="column-defer_stock_num">阶段性可挪用在库存</th>
        </tr>
        </thead>

        <!--classifySku-->
        @foreach($classifySku as $row)
            <tr>
                <td class="column-checkbox1"></td>
                <td class="column-classify-title column-text" style="padding-top: 16px;visibility: hidden;"></td>
                <td class="column-area"></td>
                <td class="column-sku-psku_code">{{ $row->sku->psku_code }}</td>
                <td class="column-sku-psku_cn column-text" style="padding-top: 9px;" title="{{ $row->sku->psku_cn ?? '' }}">{{ $row->sku->psku_cn }}</td>
                <td class="column-recommend_num">{{ $row->recommend_num ?? 0 }}</td>
                <td class="column-apply_num">{{ $row->apply_num ?? 0 }}</td>
                <td class="column-apply_remark">{{ $row->apply_remark ?? '' }}</td>
                <td class="column-approval_num">{{ $row->approval_num ?? 0 }}</td>
                <td class="column-approval_remark">{{ $row->approval_remark ?? '' }}</td>
                <td class="column-confirm_num">{{ $row->confirm_num ?? 0 }}</td>
                <td class="column-confirm_remark">{{ $row->confirm_remark ?? '' }}</td>
                <td class="column-status"></td>

                <td class="column-current_sale_ratio">{{ $row->current_sale_ratio ?? 0 }}</td>
                <td class="column-volume3_avg">{{ $row->volume3_avg ?? 0 }}</td>
                <td class="column-volume30_avg">{{ $row->volume30_avg ?? 0 }}</td>
                <td class="column-volume30_ratio">{{ $row->volume30_ratio ?? 0 }}</td>
                <td class="column-ratio">{{ $row->ratio.'%' ?? '0%' }}</td>

                <td class="column-fba_instant_num">{{ $row->fba_instant_num }}</td>
                <td class="column-fba_transit_num">{{ $row->fba_transit_num }}</td>
                <td class="column-cn_instant_num">{{ $row->cn_instant_num }}</td>
                <td class="column-cn_transit_num">{{ $row->cn_transit_num }}</td>
                <td class="column-os_instant_num">{{ $row->os_instant_num }}</td>
                <td class="column-os_transit_num">{{ $row->os_transit_num }}</td>
                <td class="column-dispense_num">{{ $row->dispense_num }}</td>
                <td class="column-dispense_surplus_num">{{ $row->dispense_surplus_num }}</td>
                <td class="column-stock_total">{{ $row->stock_total }}</td>

                @for($i = 0; $i < App\Models\SsBasic\AreaSkuEstimate::NEW_ESTIMATE_NUM; $i++)
                    <td class="column-estimate_num{{$i}}">{{ $row->{'estimate_num'.$i} }}</td>
                @endfor
                <td class="column-estimate_total">{{ $row->estimate_total ?? 0 }}</td>
                <td class="column-estimate_growth_rate">{{ $row->estimate_growth_rate ? $row->estimate_growth_rate.'%' : '0%' }}</td>

                <td class="column-cn_pulic_instant_num">{{ $row->cn_pulic_instant_num }}</td>
                <td class="column-cn_stock_total">{{ $row->cn_stock_total }}</td>
                <td class="column-overstock_num">{{ $row->overstock_num }}</td>
                <td class="column-divert_overstock_num">{{ $row->divert_overstock_num }}</td>
                <td class="column-divert_instant_num">{{ $row->divert_instant_num }}</td>
                <td class="column-divert_transit_num">{{ $row->divert_transit_num }}</td>
                <td class="column-defer_stock_num">{{ $row->defer_stock_num }}</td>
            </tr>
        @endforeach

    <!--classifyArea-->
        @foreach($classifyArea as $k => $v)
            <tr>
                <th class="column-checkbox1">
                    <input type="checkbox" class="grid-row-checkbox"
                           data-classify-id="{{ $v->classify_id }}"
                           data-area-id="{{ $v->area_id }}"
                           id="{{ 'checkbox-' . $v->classify_id . $v->area_id }}" />
                </th>
                <th class="column-classify-title column-text" style="padding-top: 16px;display: inherit;"></th>
                <th class="column-area">{{ $v->area->area ?? '' }}</th>
                <th class="column-sku-psku_code"></th>
                <th class="column-sku-psku_cn column-text" style="padding-top: 15px;"></th>
                <th class="column-recommend_num">{{  $v->recommend_num ?? 0 }}</th>
                <th class="column-apply_num">{{ $v->apply_num ?? 0 }}</th>
                <th class="column-apply_remark">{{ '' }}</th>
                <th class="column-approval_num">{{ $v->approval_num ?? 0 }}</th>
                <th class="column-approval_remark">{{ '' }}</th>
                <th class="column-confirm_num">{{ $v->confirm_num ?? 0 }}</th>
                <th class="column-confirm_remark">{{ '' }}</th>
                <th class="column-status"></th>

                <th class="column-current_sale_ratio">{{ $v->current_sale_ratio ?? 0 }}</th>
                <th class="column-volume3_avg">{{ $v->volume3_avg ?? 0  }}</th>
                <th class="column-volume30_avg">{{ $v->volume30_avg ?? 0  }}</th>
                <th class="column-volume30_ratio">{{ $v->volume30_ratio ?? 0  }}</th>
                <th class="column-ratio">{{ $v->ratio.'%' ?? '0%'  }}</th>

                <th class="column-fba_instant_num">{{ $v->fba_instant_num }}</th>
                <th class="column-fba_transit_num">{{ $v->fba_transit_num }}</th>
                <th class="column-cn_instant_num">{{ $v->cn_instant_num }}</th>
                <th class="column-cn_transit_num">{{ $v->cn_transit_num }}</th>
                <th class="column-os_instant_num">{{ $v->os_instant_num }}</th>
                <th class="column-os_transit_num">{{ $v->os_transit_num }}</th>
                <th class="column-dispense_num">{{ $v->dispense_num }}</th>
                <th class="column-dispense_surplus_num">{{ $v->dispense_surplus_num }}</th>
                <th class="column-stock_total">{{ $v->stock_total }}</th>

                @for($i = 0; $i < App\Models\SsBasic\AreaSkuEstimate::NEW_ESTIMATE_NUM; $i++)
                    <th class="column-estimate_num{{$i}}">{{ $v->{'estimate_num'.$i} }}</th>
                @endfor
                <th class="column-estimate_total">{{ $v->estimate_total ?? 0 }}</th>
                <th class="column-estimate_growth_rate">{{ $v->estimate_growth_rate ? $v->estimate_growth_rate.'%' : '0%' }}</th>

                <th class="column-cn_pulic_instant_num">{{ $v->cn_pulic_instant_num }}</th>
                <th class="column-cn_stock_total">{{ $v->cn_stock_total }}</th>
                <th class="column-overstock_num">{{ $v->overstock_num }}</th>
                <th class="column-divert_overstock_num">{{ $v->divert_overstock_num }}</th>
                <th class="column-divert_instant_num">{{ $v->divert_instant_num }}</th>
                <th class="column-divert_transit_num">{{ $v->divert_transit_num }}</th>
                <th class="column-defer_stock_num">{{ $v->defer_stock_num }}</th>
            </tr>

            <!--classifyAreaSku-->
            <?php $classifyAreaSku = \App\Models\ProductStockup\Stockup::getClassifyAreaSku($v->classify_id, $v->area_id); ?>
            @foreach($classifyAreaSku as $k1 => $v1)
                <tr>
                    <input type="hidden" name="this_psku_id_{{ $v->classify_id . $v->area_id }}" value="{{ $v1->psku_id }}">
                    <input type="hidden" id="this_stockup_id_{{ $v->classify_id . $v->area_id . $v1->psku_id }}" value="{{ $v1->id }}">
                    <td class="column-checkbox1"></td>
                    <td class="column-classify-title column-text" style="padding-top: 16px;"></td>
                    <td class="column-area"></td>
                    <td class="column-sku-psku_code">{{ $v1->sku->psku_code ?? '' }}</td>
                    <td class="column-sku-psku_cn column-text" style="padding-top: 9px" title="{{ $v1->sku->psku_cn ?? '' }}">{{ $v1->sku->psku_cn ?? '' }}</td>
                    <td class="column-recommend_num">{{  $v1->recommend_num ?? 0 }}</td>
                    <td class="column-apply_num">
                        @if($v1->status < \App\Models\ProductStockup\Stockup::STATUS_APPROVE)
                            <a class="grid-editable-apply_num" href="#" data-type="text"
                               id="{{ 'this_num_'.$v->classify_id . $v->area_id . $v1->psku_id }}"
                               data-pk="{{ $v1->id }}"
                               data-url="{{ '/xqc/product-stockup/stockup/'.$v1->id }}"
                               data-value="{{ $v1->apply_num ?? $v1->recommend_num }}">
                                {{ $v1->apply_num ?? $v1->recommend_num }}
                            </a>
                        @else
                            {{ $v1->apply_num ?? 0 }}
                        @endif
                    </td>
                    <td class="column-apply_remark">
                        @if($v1->status < \App\Models\ProductStockup\Stockup::STATUS_APPROVE)
                            <a class="grid-editable-apply_remark" href="#" data-type="text"
                               id="{{ 'this_remark_'.$v->classify_id . $v->area_id . $v1->psku_id }}"
                               data-pk="{{ $v1->id }}"
                               data-url="{{ '/xqc/product-stockup/stockup/'.$v1->id }}"
                               data-value="{{ $v1->apply_remark ?? '' }}">
                                {{ $v1->apply_remark ?? '' }}
                            </a>
                        @else
                            {{ $v1->apply_remark ?? '' }}
                        @endif
                    </td>
                    <td class="column-approval_num">{{ $v1->approval_num ?? 0 }}</td>
                    <td class="column-approval_remark">{{ $v1->approval_remark ?? '' }}</td>
                    <td class="column-confirm_num">{{ $v1->confirm_num ?? 0 }}</td>
                    <td class="column-confirm_remark">{{ $v1->confirm_remark ?? '' }}</td>
                    <td class="column-status">{{ \App\Models\ProductStockup\Stockup::$APPLY_STATUS[$v1->status] ?? '' }}</td>

                    <td class="column-current_sale_ratio">{{ $v1->current_sale_ratio ?? 0  }}</td>
                    <td class="column-volume3_avg">{{ $v1->volume3_avg ?? 0  }}</td>
                    <td class="column-volume30_avg">{{ $v1->volume30_avg ?? 0  }}</td>
                    <td class="column-volume30_ratio">{{ $v1->volume30_ratio ?? 0  }}</td>
                    <td class="column-ratio">
                        @if($v1->status == \App\Models\ProductStockup\Stockup::STATUS_APPLY)
                            <a class="grid-editable-ratio" href="#" data-type="text"
                               id="{{ 'this_ratio_'.$v->classify_id . $v->area_id . $v1->psku_id }}"
                               data-pk="{{ $v1->id }}"
                               data-url="{{ '/xqc/product-stockup/stockup/'.$v1->id }}"
                               data-value="{{ $v1->ratio ?? 0 }}">
                                {{ $v1->ratio ? $v1->ratio.'%' : '0%' }}
                            </a>
                        @else
                            {{ $v1->ratio ? $v1->ratio.'%' : '0%' }}
                        @endif
                    </td>

                    <td class="column-fba_instant_num">{{ $v1->fba_instant_num }}</td>
                    <td class="column-fba_transit_num">{{ $v1->fba_transit_num }}</td>
                    <td class="column-cn_instant_num">{{ $v1->cn_instant_num }}</td>
                    <td class="column-cn_transit_num">{{ $v1->cn_transit_num }}</td>
                    <td class="column-os_instant_num">{{ $v1->os_instant_num }}</td>
                    <td class="column-os_transit_num">{{ $v1->os_transit_num }}</td>
                    <td class="column-dispense_num">{{ $v1->dispense_num }}</td>
                    <td class="column-dispense_surplus_num">{{ $v1->dispense_surplus_num }}</td>
                    <td class="column-stock_total">{{ $v1->stock_total }}</td>

                    @for($i = 0; $i < App\Models\SsBasic\AreaSkuEstimate::NEW_ESTIMATE_NUM; $i++)
                        <td class="column-estimate_num{{$i}}">{{ $v1->{'estimate_num'.$i} }}</td>
                    @endfor
                    <td class="column-estimate_total">{{ $v1->estimate_total ?? 0 }}</td>
                    <td class="column-estimate_growth_rate">{{ $v1->estimate_growth_rate ? $v1->estimate_growth_rate.'%' : '0%' }}</td>

                    <td class="column-cn_pulic_instant_num">{{ $v1->cn_pulic_instant_num }}</td>
                    <td class="column-cn_stock_total">{{ $v1->cn_stock_total }}</td>
                    <td class="column-overstock_num">{{ $v1->overstock_num }}</td>
                    <td class="column-divert_overstock_num">{{ $v1->divert_overstock_num }}</td>
                    <td class="column-divert_instant_num">{{ $v1->divert_instant_num }}</td>
                    <td class="column-divert_transit_num">{{ $v1->divert_transit_num }}</td>
                    <td class="column-defer_stock_num">{{ $v1->defer_stock_num }}</td>
                </tr>
            @endforeach

        @endforeach
    </table>
</div>

<script language="JavaScript" type="text/javascript">
    $(function () {

        //单行选择框
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {

            let id = $(this).data('id');
            if (this.checked) {
                $.admin.grid.select(id);
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $.admin.grid.unselect(id);
                $(this).closest('tr').css('background-color', '');
            }

        }).on('ifClicked', function () {
            let id = $(this).data('id');

            if (this.checked) {
                $.admin.grid.unselect(id);
            }else {
                $.admin.grid.select(id);
            }
        });

        //全选
        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        }).on('ifClicked', function () {
            if (this.checked) {
                $.admin.grid.selects = {};
            } else {
                $('.grid-row-checkbox').each(function () {
                    var id = $(this).data('id');
                    $.admin.grid.select(id);
                });
            }
        });

        //更新
        let _editable = function (name,showbuttons = false,mode = "inline") {
            $('.grid-editable-'+name).editable({
                "emptytext":"<i class=\"fa fa-pencil\"><\/i>",
                "showbuttons":showbuttons,
                "mode":mode,
                "name":name,
                success: function(response, newValue) {
                    if (typeof response === 'object') {
                        if (response.status) {
                            $.pjax.reload('#pjax-container');
                            toastr.success(response.message);
                        } else {
                            swal(response.message, '', 'error');
                        }
                    }
                }
            });
        };
        _editable('apply_num'); //更新数量
        _editable('apply_remark'); //更新备注
        _editable('ratio'); //更新确定后的销售比例

    });
</script>
